package goulu.bloggy.com.Activities;

import goulu.bloggy.com.R;
import goulu.bloggy.com.Connections.AccountMysqlConnection;
import goulu.bloggy.com.Helpers.LoginHelper;
import goulu.bloggy.com.Models.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener
{
	private EditText usernameInput;
	private EditText passwordInput;
	
	private String password = null;
	private String username = null;
	
	public Handler loginHandler = new Handler() 
	{
		@Override
		public void handleMessage(Message msg) 
		{
			if(msg.what == AccountMysqlConnection.GET_ACCOUNT)
			{
				User user = (User)msg.obj;
				if(LoginHelper.IsLoginCorrect(username, user.getUsername(), password, user.getPassword()))
				{
					Toast.makeText(getApplicationContext(), "Login successful!", Toast.LENGTH_LONG).show();
					Intent intent = new Intent(getApplicationContext(), ViewPostsActivity.class);
					intent.putExtra("user", user);
					startActivity(intent);
				}
				else
				{
					user = null;
					Toast.makeText(getApplicationContext(), "Wrong login information!!", Toast.LENGTH_LONG).show();
				}
			}
			else if(msg.what == AccountMysqlConnection.ERROR)
			{
				Toast.makeText(getApplicationContext(), (String)msg.obj, Toast.LENGTH_LONG).show();
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginview);
		this.setupView();
	}

	private void setupView() 
	{
		Button loginBT = (Button)findViewById(R.id.loginBT);
		loginBT.setOnClickListener(this);
		Button createAccBT = (Button)findViewById(R.id.loginCreateAccBT);
		createAccBT.setOnClickListener(this);
		Button viewblogsBT = (Button)findViewById(R.id.loginViewBlogsBT);
		viewblogsBT.setOnClickListener(this);
		usernameInput = (EditText)findViewById(R.id.loginusernameInput);
		passwordInput = (EditText)findViewById(R.id.loginpasswordInput);
		
	}

	@Override
	public void onClick(View v) 
	{
		switch (v.getId()) 
		{
		case R.id.loginCreateAccBT:
			Intent intent = new Intent(getApplicationContext(), NewAccountActivity.class);
			startActivity(intent);
			break;
		case R.id.loginBT:
			this.checkInput();
			break;
		case R.id.loginViewBlogsBT:
			Intent intent2 = new Intent(getApplicationContext(), BrowseBlogsActivity.class);
			startActivity(intent2);
			break;

		}
		
	}

	private void checkInput() 
	{
		username = usernameInput.getText().toString();
		password = LoginHelper.Encrypt(passwordInput.getText().toString());
		usernameInput.setText("");
		passwordInput.setText("");
		AccountMysqlConnection conn = new AccountMysqlConnection(loginHandler, AccountMysqlConnection.GET_ACCOUNT, username);
		Thread t = new Thread(conn);
		t.start();
	}


}
